package com.cloud.ceshi.controller;

import com.cloud.ceshi.service.IPOrderService;
import com.cloud.common.core.exception.InventoryShortageException;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.ceshi.mode.dto.PlaceOrderRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zwb
 * @since 2025-07-07
 */
@RestController
@RequestMapping("/p-order")
public class POrderController {
    @Resource
    private IPOrderService orderService;

    @PostMapping("/placeOrder")
    public String placeOrder(@Validated @RequestBody PlaceOrderRequest request){
        orderService.placeOrder(request);
        return "下单成功";
    }

    @PostMapping("/test1")
    public String test1() throws InventoryShortageException {
        // 商品单价10元，库存20个,用户余额50元，模拟一次性购买22个。 期望异常回滚
        orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 22));
        return "下单成功";
    }

    @PostMapping("/test2")
    public String test2() throws InventoryShortageException {
        // 商品单价10元，库存20个，用户余额50元，模拟一次性购买6个。 期望异常回滚
        orderService.placeOrder(new PlaceOrderRequest(1L, 1L, 6));
        return "下单成功";
    }
}
